<template>
  <div id="SpaceManage">
    <a-flex justify="space-between">
      <h2>空间成员管理</h2>
    </a-flex>
    <!--  添加表单  -->
    <a-form layout="inline" :model="formData" @finish="handleSubmit">
      <a-form-item label="用户 id" name="userId">
        <a-input v-model:value="formData.userId" placeholder="请输入用户 id" allow-clear />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">添加用户</a-button>
      </a-form-item>
    </a-form>
    <div style="margin-bottom: 16px"></div>
    <!--表格-->
    <a-table :columns="columns" :data-source="spaceUserData">
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'userInfo'">
          <a-space>
            <a-avatar :src="record.userVO?.avatar" />
            {{ record.userVO?.nickname }}
          </a-space>
        </template>
        <template v-if="column.dataIndex === 'spaceRole'">
          <a-select
            v-model:value="record.spaceRole"
            :options="SPACE_ROLE_OPTIONS"
            @change="(value) => editSpaceRole(value, record)"
          />
        </template>
        <template v-else-if="column.dataIndex === 'createTime'">
          {{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-else-if="column.key === 'action'">
          <a-space wrap>
            <a-button type="link" danger @click="doDelete(record.id)">删除</a-button>
          </a-space>
        </template>
      </template>
    </a-table>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { message } from 'ant-design-vue'
import { deleteSpaceUsingPost, editSpaceUsingPost } from '@/api/spaceController.ts'
import { SPACE_ROLE_ENUM, SPACE_ROLE_OPTIONS } from '@/constants/space.ts'
import { addSpaceUserUsingPost, listSpaceUserVoUsingPost } from '@/api/spaceUserController.ts'
import dayjs from 'dayjs'

interface Props {
  id: string
}

const props = defineProps<Props>()

// 表格列
const columns = [
  {
    title: '用户',
    dataIndex: 'userInfo',
  },
  {
    title: '角色',
    dataIndex: 'spaceRole',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '操作',
    key: 'action',
  },
]

// 图片列表
const spaceUserData = ref<API.SpaceUserVO[]>([])
const formData = reactive<API.SpaceUserAddReq>({})

// 获取图片数据
const fetchData = async () => {
  const spaceId = props.id
  if (!spaceId) {
    return
  }
  const res = await listSpaceUserVoUsingPost({
    spaceId,
  })
  spaceUserData.value = res.data ?? []
}
const handleSubmit = async () => {
  const spaceId = props.id
  if (!spaceId) {
    return
  }
  await addSpaceUserUsingPost({
    spaceId,
    spaceRole: SPACE_ROLE_ENUM.VIEWER,
    ...formData,
  })
  message.success('添加成功')
  fetchData()
}
const doDelete = async (id: string) => {
  if (id) {
    await deleteSpaceUsingPost({ id })
    message.success('成员移除成功')
    fetchData()
  }
}
const editSpaceRole = async (value, record) => {
  await editSpaceUsingPost({ id: record.id, spaceRole: value })
  message.success('角色修改成功')
  fetchData()
}
onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.searchForm {
  margin-bottom: 16px;
}
</style>
